package fivestar.ui.agency;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;

import org.jdesktop.swingx.JXDatePicker;
import org.kazao.calendar.KazaoCalendarDate;
import org.netbeans.validation.api.ValidatorUtils;
import org.netbeans.validation.api.builtin.stringvalidation.StringValidators;
import org.netbeans.validation.api.ui.ValidationGroup;
import org.netbeans.validation.api.ui.swing.ValidationPanel;
import org.pushingpixels.substance.api.skin.SubstanceBusinessBlackSteelLookAndFeel;

import com.toedter.calendar.JDateChooser;

public class Register extends javax.swing.JDialog implements ActionListener,
		ItemListener, KeyListener {

	private static final long serialVersionUID = -4800414210063077974L;

	private JLabel lblNif, lblEmail, lblCompany, lblName, lblQuota,
			lblAddress, lblDistrict, lblCity, lblCountry, lblZipCode, lblPhone;
	JLabel lblAgency;
	JLabel lblNewLabel;
	private JTextField txtNif, txtEmail, txtAddress, txtDistrict, txtZipCode,
			txtPhone, txtCompany, txtName;
	private JButton btnAdd, btnCancel;
	JButton btnNewButton;
	JButton button;
	@SuppressWarnings("rawtypes")
	private JComboBox cboCountry, cboCity;
	private JPanel pnlGuest,pnlTitle,pnlPersonal, pnlLocation, pnlOption;
	JSeparator separator;
	String ruta = "No tiene";
	String country = "";

	private KazaoCalendarDate txtBirthDate;
	private JXDatePicker datePicker;

	private JLabel lblDiscount;
	private JLabel lblMobile;
	private JTextField txtMobile;

	private JLabel lblFax;
	private JTextField txtFax;
	private ButtonGroup groupGender;
	ValidationPanel pnlValidation;
	ValidationGroup group;
	private JTextField txtDiscount;
	private JTextField txtQuota;
	

	public Register(Frame parent, boolean modal) {
		super(parent, modal);
		setResizable(false);
		initComponents();
		this.setLocationRelativeTo(null);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initComponents() {
		try {

			getContentPane().setLayout(null);

			pnlGuest = new JPanel();
			pnlGuest.setBounds(5, 5, 479, 441);
			pnlGuest.setLayout(null);
			getContentPane().add(pnlGuest);

			pnlValidation = new ValidationPanel();
			pnlValidation.setBounds(0, 0, 489, 419);
			pnlValidation.setInnerComponent(pnlGuest);
			getContentPane().add(pnlValidation);
			group = pnlValidation.getValidationGroup();

			pnlTitle = new JPanel();
			pnlTitle.setForeground(new Color(0, 0, 0));
			pnlTitle.setBounds(10, 0, 464, 67);
			pnlGuest.add(pnlTitle);
			pnlTitle.setLayout(null);

			separator = new JSeparator();
			separator.setBounds(5, 36, 454, 2);
			pnlTitle.add(separator);

			lblAgency = new JLabel("Agency");
			lblAgency.setHorizontalAlignment(SwingConstants.LEFT);
			lblAgency.setVerticalAlignment(SwingConstants.TOP);
			lblAgency.setFont(new Font("Tahoma", Font.PLAIN, 30));
			lblAgency.setBounds(10, 0, 233, 38);
			pnlTitle.add(lblAgency);

			lblNewLabel = new JLabel("Register");
			lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
			lblNewLabel.setVerticalAlignment(SwingConstants.BOTTOM);
			lblNewLabel.setForeground(new Color(0, 0, 0));
			lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 20));
			lblNewLabel.setBounds(10, 36, 233, 27);
			pnlTitle.add(lblNewLabel);

			// Tabla personal information
			pnlPersonal = new JPanel();
			pnlPersonal.setForeground(new Color(0, 0, 0));
			pnlPersonal.setBounds(10, 76, 464, 125);
			pnlGuest.add(pnlPersonal);
			pnlPersonal.setBorder(BorderFactory.createTitledBorder(null,
					"Personal", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Tahoma", 0, 10)));
			pnlPersonal.setLayout(null);

			groupGender = new ButtonGroup();
			lblNif = new JLabel("Nif");
			lblNif.setHorizontalAlignment(SwingConstants.TRAILING);
			lblNif.setBounds(10, 23, 62, 26);
			lblNif.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlPersonal.add(lblNif);

			txtNif = new JTextField();
			txtNif.setBounds(78, 23, 147, 27);
			txtNif.setFont(new Font("Tahoma", Font.PLAIN, 11));
			txtNif.addKeyListener(this);
			txtNif.setName("Nif");
			pnlPersonal.add(txtNif);

			lblName = new JLabel("Name");
			lblName.setHorizontalAlignment(SwingConstants.TRAILING);
			lblName.setBounds(233, 23, 62, 26);
			lblName.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlPersonal.add(lblName);

			txtName = new JTextField();
			txtName.setBounds(301, 23, 147, 27);
			txtName.setName("Name");
			txtName.setFont(new Font("Tahoma", Font.PLAIN, 11));
			txtName.addKeyListener(this);
			txtName.addActionListener(this);
			pnlPersonal.add(txtName);

			lblCompany = new JLabel();
			lblCompany.setHorizontalAlignment(SwingConstants.TRAILING);
			lblCompany.setText("Company");
			lblCompany.setBounds(10, 51, 62, 26);
			lblCompany.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlPersonal.add(lblCompany);

			txtCompany = new JTextField();
			txtCompany.setBounds(78, 51, 147, 27);
			txtCompany.setName("Company");
			txtCompany.setFont(new Font("Tahoma", Font.PLAIN, 11));
			txtCompany.addKeyListener(this);
			txtCompany.addActionListener(this);
			pnlPersonal.add(txtCompany);

			lblEmail = new JLabel("E-mail");
			lblEmail.setHorizontalAlignment(SwingConstants.TRAILING);
			lblEmail.setBounds(233, 51, 62, 26);
			lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlPersonal.add(lblEmail);

			txtEmail = new JTextField();
			txtEmail.setBounds(301, 51, 147, 27);
			txtEmail.setFont(new Font("Tahoma", Font.PLAIN, 11));
			txtEmail.addKeyListener(this);
			txtEmail.addActionListener(this);
			txtEmail.setName("Email Address");
			pnlPersonal.add(txtEmail);

			lblQuota = new JLabel("Quota");
			lblQuota.setHorizontalAlignment(SwingConstants.TRAILING);
			lblQuota.setBounds(233, 79, 62, 26);
			lblQuota.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlPersonal.add(lblQuota);

			lblDiscount = new JLabel("Discount");
			lblDiscount.setHorizontalAlignment(SwingConstants.TRAILING);
			lblDiscount.setFont(new Font("Tahoma", Font.PLAIN, 11));
			lblDiscount.setBounds(10, 79, 62, 26);
			pnlPersonal.add(lblDiscount);

			// Tabla Location
			pnlLocation = new JPanel();
			pnlLocation.setBounds(10, 201, 464, 151);
			pnlGuest.add(pnlLocation);
			pnlLocation.setBorder(BorderFactory.createTitledBorder(null,
					"Location", TitledBorder.DEFAULT_JUSTIFICATION,
					TitledBorder.DEFAULT_POSITION, new Font("Tahoma", 0, 10)));
			pnlLocation.setLayout(null);

			lblAddress = new JLabel("Address ");
			lblAddress.setHorizontalAlignment(SwingConstants.TRAILING);
			lblAddress.setBounds(10, 23, 62, 26);
			lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlLocation.add(lblAddress);

			txtAddress = new JTextField();
			txtAddress.setBounds(78, 23, 147, 27);
			txtAddress.setName("Address");
			txtAddress.addKeyListener(this);
			txtAddress.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlLocation.add(txtAddress);

			lblCity = new JLabel("City");
			lblCity.setHorizontalAlignment(SwingConstants.TRAILING);
			lblCity.setBounds(10, 51, 62, 26);
			lblCity.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlLocation.add(lblCity);

			cboCity = new JComboBox();
			cboCity.setBounds(78, 51, 123, 27);
			cboCity.setFont(new Font("Tahoma", Font.PLAIN, 11));
			cboCity.addItemListener(this);
			cboCity.setName("City");
			pnlLocation.add(cboCity);

			btnNewButton = new JButton("New button");
			btnNewButton.setBounds(201, 51, 23, 27);
			pnlLocation.add(btnNewButton);

			lblCountry = new JLabel("Country");
			lblCountry.setHorizontalAlignment(SwingConstants.TRAILING);
			lblCountry.setBounds(230, 23, 62, 26);
			lblCountry.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlLocation.add(lblCountry);

			cboCountry = new JComboBox();
			cboCountry.setBounds(301, 23, 123, 27);
			cboCountry.setFont(new Font("Tahoma", Font.PLAIN, 11));
			cboCountry.addItemListener(this);
			cboCountry.setName("Country");
			pnlLocation.add(cboCountry);

			button = new JButton("New button");
			button.setBounds(424, 23, 23, 27);
			pnlLocation.add(button);

			lblZipCode = new JLabel("Zip Code");
			lblZipCode.setHorizontalAlignment(SwingConstants.TRAILING);
			lblZipCode.setBounds(10, 79, 62, 26);
			lblZipCode.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlLocation.add(lblZipCode);

			txtZipCode = new JTextField();
			txtZipCode.setBounds(78, 79, 147, 27);
			txtZipCode.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlLocation.add(txtZipCode);

			lblPhone = new JLabel("Phone");
			lblPhone.setHorizontalAlignment(SwingConstants.TRAILING);
			lblPhone.setBounds(230, 79, 62, 26);
			lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlLocation.add(lblPhone);

			txtPhone = new JTextField();
			txtPhone.setBounds(301, 79, 147, 27);
			txtPhone.setName("Phone");
			txtPhone.addKeyListener(this);
			txtPhone.setFont(new Font("Tahoma", Font.PLAIN, 11));
			pnlLocation.add(txtPhone);

			lblDistrict = new JLabel("District");
			lblDistrict.setHorizontalAlignment(SwingConstants.TRAILING);
			lblDistrict.setBounds(230, 51, 62, 26);
			pnlLocation.add(lblDistrict);
			lblDistrict.setFont(new Font("Tahoma", Font.PLAIN, 11));

			txtDistrict = new JTextField();
			txtDistrict.setBounds(301, 51, 147, 27);
			txtDistrict.setName("District");
			pnlLocation.add(txtDistrict);
			txtDistrict.setFont(new Font("Tahoma", Font.PLAIN, 11));
			txtDistrict.addKeyListener(this);

			lblMobile = new JLabel("Mobile");
			lblMobile.setHorizontalAlignment(SwingConstants.TRAILING);
			lblMobile.setFont(new Font("Tahoma", Font.PLAIN, 11));
			lblMobile.setBounds(10, 106, 62, 26);
			pnlLocation.add(lblMobile);

			txtMobile = new JTextField();
			txtMobile.setName("Mobile");
			txtMobile.addKeyListener(this);
			txtMobile.setFont(new Font("Tahoma", Font.PLAIN, 11));
			txtMobile.setBounds(78, 106, 147, 27);
			pnlLocation.add(txtMobile);

			lblFax = new JLabel("Fax");
			lblFax.setHorizontalAlignment(SwingConstants.TRAILING);
			lblFax.setFont(new Font("Tahoma", Font.PLAIN, 11));
			lblFax.setBounds(230, 106, 62, 26);
			pnlLocation.add(lblFax);

			txtFax = new JTextField();
			txtFax.setFont(new Font("Tahoma", Font.PLAIN, 11));
			txtFax.setBounds(301, 106, 147, 27);
			pnlLocation.add(txtFax);

			pnlOption = new JPanel();
			pnlOption.setBounds(0, 360, 480, 30);
			pnlGuest.add(pnlOption);
			pnlOption.setLayout(null);

			btnAdd = new JButton("Add");
			btnAdd.setEnabled(false);
			btnAdd.setBounds(254, 0, 98, 28);
			pnlOption.add(btnAdd);
			btnAdd.setFont(new Font("Tahoma", Font.PLAIN, 10));
			btnAdd.addActionListener(this);

			btnCancel = new JButton("Cancel");
			btnCancel.setBounds(362, 0, 98, 28);
			pnlOption.add(btnCancel);
			btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 10));
			btnCancel.addActionListener(this);

			group.add(txtMobile, ValidatorUtils.merge(
					StringValidators.REQUIRE_NON_EMPTY_STRING,
					StringValidators.REQUIRE_VALID_NUMBER,
					StringValidators.REQUIRE_NON_NEGATIVE_NUMBER));
			group.add(txtPhone, ValidatorUtils.merge(
					StringValidators.REQUIRE_NON_EMPTY_STRING,
					StringValidators.REQUIRE_VALID_NUMBER,
					StringValidators.REQUIRE_NON_NEGATIVE_NUMBER));
			group.add(txtDistrict, ValidatorUtils
					.merge(StringValidators.REQUIRE_NON_EMPTY_STRING));
			group.add(cboCity, ValidatorUtils
 					.merge(StringValidators.REQUIRE_NON_EMPTY_STRING));	
			group.add(cboCountry, ValidatorUtils
 					.merge(StringValidators.REQUIRE_NON_EMPTY_STRING));	
			group.add(txtAddress, ValidatorUtils
					.merge(StringValidators.REQUIRE_NON_EMPTY_STRING));			
			group.add(txtEmail, ValidatorUtils.merge(
					StringValidators.REQUIRE_NON_EMPTY_STRING, ValidatorUtils
							.merge(StringValidators.NO_WHITESPACE,
									StringValidators.EMAIL_ADDRESS)));
			group.add(txtCompany, ValidatorUtils
					.merge(StringValidators.REQUIRE_NON_EMPTY_STRING));
			group.add(txtName, ValidatorUtils
					.merge(StringValidators.REQUIRE_NON_EMPTY_STRING));
			group.add(txtNif, ValidatorUtils.merge(
					StringValidators.REQUIRE_NON_EMPTY_STRING,
					StringValidators.NO_WHITESPACE,
					StringValidators.REQUIRE_NON_NEGATIVE_NUMBER));
			
			txtDiscount = new JTextField();
			txtDiscount.setName("Discount");
			txtDiscount.setFont(new Font("Tahoma", Font.PLAIN, 11));
			txtDiscount.setBounds(78, 79, 147, 27);
			pnlPersonal.add(txtDiscount);
			
			txtQuota = new JTextField();
			txtQuota.setName("Quota");
			txtQuota.setFont(new Font("Tahoma", Font.PLAIN, 11));
			txtQuota.setBounds(301, 79, 147, 27);
			pnlPersonal.add(txtQuota);

			setSize(495, 455);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void keyPressed(KeyEvent evt) {
		// TODO Auto-generated method stub
		// if(evt.getSource()== txtDNI){
		// if(!(txtDNI.getText().equals("")))
		// btnAdd.setEnabled(true);
		// else{
		// btnAdd.setEnabled(false);
		// }
		// }
	}

	@Override
	public void keyReleased(KeyEvent evt) {
		// TODO Auto-generated method stub
		// if(evt.getSource()== txtDNI){
		if (txtNif.getText().equals("") || txtName.getText().equals("")
				|| txtCompany.getText().equals("")
				|| txtEmail.getText().equals("")
				|| txtAddress.getText().equals("")
				|| txtDistrict.getText().equals("")
				|| txtPhone.getText().equals("")
				|| txtMobile.getText().equals("")) {
			btnAdd.setEnabled(false);
		} else {
			btnAdd.setEnabled(true);
		}
		// }
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//JRibbonFrame.setDefaultLookAndFeelDecorated(true);
					//JDialog.setDefaultLookAndFeelDecorated(true);
					UIManager.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
				} catch (UnsupportedLookAndFeelException unsupportedLookAndFeelException) {
					System.out.println(unsupportedLookAndFeelException.getMessage());
				}
				Register dialog = new Register(null, true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);

			}
		});
	}
}
